﻿@page "/"
@using BlazorApp1.Component
@using BlazorApp1.Utils
@inject NavigationManager navigation
@inject LocalStorageAccessor LocalStorageAccessor
<PageTitle>Index</PageTitle>
@*
注释 RAZOR 的 IF文 
*@
@if (ishelloFlag)
{
    <h1>Hello, world!</h1>
}
@*
注释 RAZOR 的 FOR文
*@
@for (int i = 0; i < 2; i++)
{
@*
注释 @变量取出
*@
    <h1>@i</h1>
}
@*
注释 @bind-value 双向绑定
*@
<input type="text" @bind-value="bindvalue" />
<button @onclick=clear>清除</button>
<button @onclick=toroku>登录</button>

<br />
@*
注释 添加动态组件事件
*@
<button @onclick=insert>添加ConfirmButton标签</button>
<br/>
<button @onclick=moveto>路由</button>
<br />

@*
注释 ContentDiv 自定义容器组件 RenderFragment
*@
<ContentDiv>
    @*
    注释 CascadingValue 值传递到子容器中 参照ConfirmButton的 [CascadingParameter(Name = "ThemeName")]
    *@
  <CascadingValue Value="@CascadingValue" Name="ThemeName">
        @*
        注释 ConfirmButton 自定义button组件 属性 Parameter 事件 EventCallback
        *@
    <ConfirmButton OnConfirmed="@(() =>ishelloFlag=false)" textvalue="测试" />
 </CascadingValue>
</ContentDiv>
@if (selectedType!=null)
{
    @*
      注释 动态组件
    *@
    <DynamicComponent Type="@selectedType" Parameters="@dic" />
}

<br/>
<br/>
<form>
    <label class="form-label">
        Key
        <input class="form-control" type="text" @bind-value="Key" />
    </label>
    <label class="form-label">
        Value
        <input class="form-control" type="text" @bind-value="Value" />
    </label>
    <button class="btn btn-primary" type="button" @onclick="SetValueAsync">Set Value</button>
</form>
<div>Stored Value: @StoredValue</div>
<button class="btn btn-primary" type="button" @onclick="GetValueAsync">Get Value</button>
<button class="btn btn-primary" type="button" @onclick="RemoveAsync">Remove Value</button>
<button class="btn btn-primary" type="button" @onclick="ClearAllAsync">Clear All</button>
@code {

    private Type? selectedType;
    private Dictionary<string, object> dic =new Dictionary<string, object>();
    string CascadingValue = "ssy";
    bool ishelloFlag = true;
    string bindvalue = "xx";
    void clear()
    {
        bindvalue = "";
    }
    void toroku()
    {
        Console.WriteLine(bindvalue);
    }
    void insert()
    {
        selectedType = Type.GetType("BlazorApp1.Component.ConfirmButton");
        dic["textvalue"] = "add";

    }
    void moveto()
    {
        //路由迁移
        navigation.NavigateTo("counter/15");
    }

    public string Key { get; set; } = "";
    public string Value { get; set; } = "";
    public string StoredValue { get; set; } = "";

    public async Task SetValueAsync()
    {
        await LocalStorageAccessor.SetValueAsync(Key, Value);
    }

    public async Task GetValueAsync()
    {
        StoredValue = await LocalStorageAccessor.GetValueAsync<string>(Key);
    }

    public async Task RemoveAsync()
    {
        await LocalStorageAccessor.RemoveAsync(Key);
    }

    public async Task ClearAllAsync()
    {
        await LocalStorageAccessor.Clear();
    }
}

